<template>
  <div>
    <base-list ref="unifyGoodsAccount"
               :condition="condition"
               :datas.sync="unifyGoodsAccount"
               :pageSearch="pageSearch">
      <el-form :inline="true"
               :model="condition"
               ref="unifyform"
               class="search-form"
               slot="search"
               label-width="80px">
        <el-row>
          <el-col :span="8"
                  class="search-width4">
            <el-form-item label="审核状态">
              <el-select v-model="condition['search_EQ_status']"
                         placeholder="请选择"
                         clearable
                       filterable>
                <el-option v-for="[key, val] in _dictAdjustStatus"
                           :key="key"
                           :label="val"
                           :value="key"></el-option>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width4">
            <el-form-item label="类型">
              <el-select v-model="condition['search_EQ_adjustWay']"
                         placeholder="请选择"
                         clearable
                       filterable>
                <el-option v-for="[key, val] in _dictAdjustWay"
                           :key="key"
                           :label="val"
                           :value="key"></el-option>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8"
                  class="search-width4">
            <el-form-item label="货主">
              <auto-search-name placeholder="请选择"
                                v-model="condition['search_EQ_cargoOwnerId']"
                                @selectId="v=>condition['search_EQ_cargoOwnerId']=v"
                                :name="form.cargoOwnerName"></auto-search-name>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width8">
            <el-form-item label="调账时间">
              <el-date-picker style="width:140px;" v-model="condition['search_GTE_startTime']"
                              type="date"
                              placeholder="选择日期"
                              value-format="yyyy-MM-dd"> </el-date-picker> 至
              <el-date-picker style="width:140px;" v-model="condition['search_LTE_endTime']"
                              type="date"
                              placeholder="选择日期"
                              value-format="yyyy-MM-dd"> </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="3"
                  style="width: 190px;">
            <el-button type="primary"
                       icon="el-icon-search"
                       @click="loadData"
                       class="ml-80">查 询</el-button>
            <el-button formVisible="true"
                      @click="resultForm('unifyform')"
                      icon="el-icon-refresh">
              重置
            </el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-row slot="status">
        <el-button type="primary"
                   @click="handleFormOpen('unifyGoodsAccountForm')">新增调账</el-button>
      </el-row>
      <el-table slot="list"
                :data="unifyGoodsAccount"
                border
                stripe
                max-height="500"
                @selection-change="handleSelectionChange">
        <el-table-column type="index"
                         label="序号"
                         width="55">
          <template slot-scope="scope">
            {{ scope.$index+1 }}
          </template>
        </el-table-column>
        <el-table-column prop="id"
                         label="调账单号"
                         min-width="80"></el-table-column>
        <el-table-column prop="accountpageId"
                         label="账页号"
                         min-width="80"></el-table-column>
        <el-table-column prop="status"
                         label="状态"
                         min-width="80">
                <template slot-scope='scope'>
                    {{scope.row.status | _filterAdjustStatus }}
                </template> 
        </el-table-column>
        <el-table-column prop="adjustWay"
                         label="调账类型"
                         min-width="80">
                <template slot-scope='scope'>
                    {{scope.row.adjustWay | _filterAdjustWay }}
                </template> 
        </el-table-column>
        <el-table-column prop="weight"
                         label="调整重量"
                         min-width="80"></el-table-column>
        <el-table-column prop="num"
                         label="调整件数"
                         min-width="80"></el-table-column>
        <el-table-column prop="cargoOwnerName"
                         label="货主"
                         min-width="80"></el-table-column>
        <el-table-column prop="cargoName"
                         label="品名"
                         min-width="80"></el-table-column>
        <el-table-column prop="normOne"
                         label="规格"
                         min-width="80">
          <template slot-scope="scope">
            {{scope.row.normOne + '*' + scope.row.normTwo + '*' + scope.row.normThree}}
          </template>                
        </el-table-column>
        <el-table-column prop="material"
                         label="材质"
                         min-width="80"></el-table-column>
        <el-table-column prop="madeBy"
                         label="产地/厂家"
                         min-width="80"></el-table-column>
        <el-table-column prop="manageWay"
                         label="管理方式"
                         min-width="80">
          <template slot-scope="scope">
            {{scope.row.manageWay | _filterManagement}}
          </template>               
        </el-table-column>
        <el-table-column prop="measureWay"
                         label="计量方式"
                         min-width="80">
          <template slot-scope="scope">
            {{scope.row.measureWay | _filterMeterStyle}}
          </template> 
        </el-table-column>
        <el-table-column align="center"
                         label="操作"
                         width="140">
          <template slot-scope="scope">
            <el-button v-if="scope.row.status=='A' || scope.row.status=='D'" size="small"
                       type="text"
                       @click="handleFormOpen('unifyGoodsAccountForm',true,scope.row)">
              编辑
            </el-button>
            <el-button  v-if="scope.row.status=='A' || scope.row.status=='D'" type="text"
                       class="danger"
                       size="small"
                       @click="handleRemove('del',scope.row)">删除
            </el-button>
            <el-button v-if="scope.row.status=='A'" type="text"
                       size="small"
                       @click="handleRemove('submit',scope.row)">提交
            </el-button>
            <el-button v-if="scope.row.status=='B'" type="text"
                       size="small"
                       @click="handleRemove('apply',scope.row)">审核
            </el-button>
            <el-button v-if="scope.row.status=='B'" type="text"
                       size="small"
                       @click="handleRemove('return',scope.row)">退回
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </base-list>
    <div style="text-align:center">
      <el-button plain
                 type="info"
                 plain
                 @click="handleRouterPush({path: '/goods/storehouse',name: '仓库管理'})">返回</el-button>
    </div>
    <component :is="component"
               ref="component"
               @load="loadData"
               @clear="component=''"></component>
  </div>
</template>

<script>
import {
  findViceAccount,
  addAccountPage,
  getAccountPageList,
  getAdjustList,
  getAdjustDelete,
  getAdjustReturn,
  getAdjustCheck,
  getAdjustSubmit
} from '@/api/things/accountPageList'
import unifyGoodsAccountForm from './unifyGoodsAccountForm'
import autoSearchName from '@/components/autocomplete/autoSearchName' //货主组件
import autoCargo from '@/components/autocomplete/autoCargo'

export default {
  name: 'unifyGoodsAccount',
  components: {
    unifyGoodsAccountForm,
    autoCargo,
    autoSearchName
  },
  data() {
    return {
      form: this.clearForm(),
      dialogForm: false,
      component: '',
      condition: {search_EQ_manageWay: 'UNIFIED'}, // 查询条件
      unifyGoodsAccount: [], // table数组
      pageSearch: getAdjustList, // 分页查询api接口
      id: '',
      accountPageList: null,
      // xinjia
      manageType: '1',
      calculate: '1',
      multipleSelection: []
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.loadData()
    })
  },
  methods: {
    clearForm() {
      return {
        id: null,
        orgId: '',
        orgId:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.id,
        orgName:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.cargoName,
        // 新加
        search_EQ_adjustWay: '',
        search_EQ_status: '',
        // search_EQ_accountPageType: '', //账页类型
        // search_EQ_cargoName: '', //品名
        search_EQ_endTime: '', //建账结束时间
        search_EQ_startTime: '', //建账开始时间
        // search_EQ_Time: '', //建账时间
        // search_EQ_madeBy: '', //生产厂家
        search_EQ_manageWay: 'UNIFIED', //管理方式
        // search_EQ_material: '', //材质
        // search_EQ_measureWay: '', //计量方式
        // search_EQ_normOne: '', //规格
        // search_EQ_normThree: '',
        // search_EQ_normTwo: '',
        // search_EQ_warehouseCode: '', //所属仓库
        // search_LIKE_accountpageNo: '', //账页号
        search_EQ_cargoOwnerCode: '', //货主单位
        search_EQ_cargoOwnerId: '', //货主单位
        sortType: '' //排序
      }
    },
    handleFormOpen(ref,bool,item) {
      this.component = ref
      this.$nextTick(() => {
        if (bool) {
          let form = JSON.parse(JSON.stringify(item))
          if(form.adjustWay == 'TZRK') {
            form.adjustWay = '调整入库'
          }else{
            form.adjustWay = '调整出库'
          }
          this.$refs.component.unifyform = {
            id: form.id,  
            accountpageId: form.accountpageId,
            search_EQ_accountPageType: form.accountPageType, //账页类型
            search_EQ_cargoName: form.cargoName, //品名
            search_EQ_id: form.accountpageId, //建账结束时间
            // search_EQ_startTime: form., //建账开始时间
            search_EQ_madeBy: form.madeBy, //生产厂家
            search_EQ_manageWay: 'UNIFIED', //管理方式
            search_EQ_material: form.material, //材质
            search_EQ_measureWay: form.measureWay, //计量方式
            search_EQ_normOne: form.normOne, //规格
            search_EQ_normThree: form.normThree,
            search_EQ_normTwo: form.normTwo,
            // search_EQ_warehouseCode: form., //所属仓库
            search_LIKE_accountpageNo: form.accountpageNo, //账页号
            search_EQ_cargoOwnerCode: form.cargoOwnerCode, //货主单位
            search_EQ_cargoOwnerId: form.cargoOwnerId, //货主单位  
            search_EQ_cargoOwnerName: form.cargoOwnerName, //货主单位
            // sortType: '', //排序
            balanceWeight: form.balanceWeight,
            balanceNum: form.balanceNum,
            adjustWay: form.adjustWay,
            weight: form.weight,
            num: form.num,
            remark: form.remark
          }
          this.$refs.component.isEdit = true
          this.$refs.component.dialogForm = true
          this.$refs.component.searchunifyForm();
        } else {
          this.$refs.component.dialogForm = true
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    loadData() {
      this.$refs.unifyGoodsAccount.loadData()
    },
    handleViceAccount() {
      let params = {
        search_EQ_accountPageType: this.form.accountPageType, //账页类型
        search_EQ_cargoName: this.form.cargoName, //品名
        search_EQ_madeBy: this.form.madeBy, //生产厂家
        search_EQ_manageWay: '', //管理方式
        search_EQ_material: this.form.material, //材质
        search_EQ_measureWay: '', //计量方式
        search_EQ_normOne: this.form.normOne, //规格
        search_EQ_normThree: this.form.normThree,
        search_EQ_normTwo: this.form.normTwo,
        // search_EQ_warehouseCode: this.form.warehouseCode,  //所属仓库
        search_LIKE_accountpageNo: this.form.accountpageNo, //账页号
        search_EQ_cargoOwnerId: this.form.cargoOwnerId, //货主单位
        sortType: '' //排序
      }
      // findViceAccount(params).then(
      //   res => {
      //     this.viceAccountList = res.data.aaData
      //   }
      // )
    },
    handleRemove(type,item) {
      if(type == 'del') {
        this.$confirm(`是否删除该数据?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            getAdjustDelete(item.id).then(res => {
              this.$message({
                type: 'success',
                message: `删除成功!`
              })
              this.loadData()
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消'
            })
          })
      }
      if(type == 'return') {
        this.$prompt(`是否退回调账?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({ value }) => {
          getAdjustReturn(item.id, {returnReason: value}).then(res => {
              this.$message({
                type: 'success',
                message: `退回成功!`
              })
              this.loadData()
            })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });       
        });
      }
      if(type == 'apply') {
        this.$confirm(`是否审核该数据?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            getAdjustCheck(item.id).then(res => {
              this.$message({
                type: 'success',
                message: `审核成功!`
              })
              this.loadData()
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消'
            })
          })
      }
      
      if(type == 'submit') {
        this.$confirm(`是否提交该数据?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            getAdjustSubmit(item.id).then(res => {
              this.$message({
                type: 'success',
                message: `提交成功!`
              })
              this.loadData()
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消'
            })
          })
      }
    },
    resultForm(forms) {
      this.$refs[forms].resetFields();
      this.condition = this.clearForm();
    }
  }
}
</script>

<style>
.control-line {
  height: 30px;
}
.line {
  border-left: solid 5px #00a1ff;
  height: 18px;
  margin-top: 5px;
  display: inline-block;
  vertical-align: sub;
}
.gg-input {
  margin-bottom: 0 !important;
}
.gg-input .el-form-item__content .el-input {
  width: 100px !important;
}
.search-width6 .el-date-editor {
  width: 240px;
}
</style>
